var Crawler = require('crawler');
var fs = require('fs');

var c = new Crawler({ 
    maxConnections: 10
});

fs.stat('./data',function(err,stats){
    if(err){
        fs.mkdir('./data');
        console.log('文件创建成功')
    }
    else{
        console.log('data存在')
    }
})

var mp_videos = [{
    name:'搞笑',
    url:'http://www.meipai.com/square/13?p=',
    code:'gaoxiao'
},
{
    name:'女神',
    url:'http://www.meipai.com/square/19?p=',
    code:'nvshen'
},
{
    name:'宠物',
    url:'http://www.meipai.com/square/6?p=',
    code:'chongwu'
}]

mp_videos.forEach(function(video){
    getVideos(video.code,video.url,1);
})

function getVideos(type,url,pageCount){

    var videos = [];

    getData(url,1,pageCount);

    function getData(baseUrl,page,pageCount){
        url = baseUrl + page;
        c.queue({
            uri: url,
            callback:function(error, res, done){
                if(error){
                    console.log(error);
                }
                else{
                    var $ = res.$;

                    

                    if(page <= pageCount){

                        $('#mediasList li').each(function(){
                            videos.push(convertToVideo($(this)));
                        })

                        getData(baseUrl,page+1,pageCount);
                    }
                    else{
                        fs.writeFile(`./data/video_${type}.json`, JSON.stringify(videos));
                        console.log(`sucess...video_${type}.json`);
                    }

                }
                // console.log(videos);
                done();
            }
        })
    }

}

function convertToVideo(video) {
    var obj = {}
    obj.title = video.find('strong').text();
    obj.img = video.find('img').attr('src');
    // obj.link = 'http://www.meipai.com'+$(this).find('a').attr('href');
    obj.video = video.find('.content-l-video').data('video');
    return obj;
}


